package com.sky.service.user.impl;

import com.alibaba.fastjson.JSONObject;
import com.sky.mapper.user.UserCategoryMapper;
import com.sky.pojo.Category;
import com.sky.result.Result;
import com.sky.service.user.UserCategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class UserCategoryServiceImpl implements UserCategoryService {
    private final UserCategoryMapper userCategoryMapper;
    private final RedisTemplate redisTemplate;
    /**
     * 获取分类列表
     * @return
     */
    @Override
    public List<Category> listFromCache() {
        log.info("获取分类列表");

        List<Category> list=userCategoryMapper.getList();
        return list;
    }

    /**
     * 添加缓存数据到redis
     */
    /*@Override
    public List<Category> listFromCache1(Integer type) {
        //先从缓存获取，有数据则返回，没有则从数据库查询，再添加到缓存中
        String jsonString = (String)redisTemplate.opsForValue().get("CATEGORY_LIST");
        if (StringUtils.hasText(jsonString)){
            return JSONObject.parseArray(jsonString, Category.class);
        }
        log.info("redis中不存在分类数据，从数据库查询");
        List<Category> list = userCategoryMapper.getList(type);
        redisTemplate.opsForValue().set("CATEGORY_LIST", JSONObject.toJSONString(list));
        return list;
    }*/
}
